// pages/Census/data-detail/index.js
const Controllers = require('../../controllers/ControllerMy.js').controller;
const agentIncomeDetails = require('../../controllers/ControllerCensus').controller.agentIncomeDetails;

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        text: '收入',
        status: 0
      },
      {
        text: '支出',
        status: 1
      }
    ], 
    tabII: [
      [
        {
          text: '全部',
          status: 0
        },
        {
          text: '店铺租金',
          status: 8
        },
        {
          text: '云商品',
          status: 7
        }
      ],
      [
        {
          text: '全部',
          status: 0
        },
        {
          text: '员工提成',
          status: 2
        },
        {
          text: '提现',
          status: 5
        }
      ]
    ],
    active: 0,
    activeII: 0,
    page: {
      pageNumber: 1,
      pageSize: 10,
    },
    agentRecordData: {},
    list: [],
    totalRow: 0,
    isLoading: false,
    lastPage: true
  },
  agentIncomeDetails() {
    var that = this;
    var page = that.data.page;
    var active = that.data.active;
    var activeII = that.data.activeII;
    var tabs = that.data.tabs;
    var tabII = that.data.tabII;
    if (that.data.isLoading) {
      return;
    }

    const genre = tabs[active].status;
    const type = tabII[active][activeII].status;

    var params = { ...page, genre };
    if (type) {
      params.type = type;
    }

    that.data.isLoading = true;
    wx.showLoading();
    agentIncomeDetails(params).then(res => {
      console.log('agentIncomeDetails--res==', res);
      const { code, data, desc } = res;
      const { list, totalRow, lastPage } = data;
      if (page.pageNumber === 1) {
        that.data.list = list;
      } else {
        that.data.list = that.data.list.concat(list);
      }
      that.setData({
        list: that.data.list,
        totalRow,
        lastPage,
        isLoading: false
      });
      wx.hideLoading();
    })
  },
  
  agentRecord() {
    var that = this;
    Controllers.agentRecord({}).then(res => {
      console.log('agentRecord--res==', res);
      const { code, data, desc } = res;
      const { cloud, rent } = data;
      var totalProfit = parseFloat(cloud + rent).toFixed(2);
      that.setData({
        agentRecordData: data,
        totalProfit
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.agentRecord();
    that.agentIncomeDetails();
  },
  changeTab(e) {
    console.log(e);
    
    var that = this;
    var layer = parseInt(e.currentTarget.dataset.layer);
    var index = parseInt(e.currentTarget.dataset.index);
    if (layer === 1) {
      console.log(index, that.data.active);
      
      if (index !== that.data.active) {
        that.data.active = index;
        that.agentIncomeDetails();
      }
    } else if (layer === 2) {
      if (index !== that.data.activeII) {
        that.data.activeII = index;
        that.agentIncomeDetails();
      }
    }
    that.setData({
      active: that.data.active,
      activeII: that.data.activeII
    })
  },
  openPage(e) {
    var url = e.currentTarget.dataset.url;
    if (url) {
      app.openPage({ url });
    } else {
      wx.showToast({
        title: '未获取页面路径',
        icon: 'none'
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.isLoading || that.data.lastPage) {
      return;
    }
    that.data.page.pageNumber += 1;
    that.agentIncomeDetails();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})